﻿using Client.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {

        }
        #region 登录按钮
        private void labelControl1_Click(object sender, EventArgs e)
        {
            GetLogin();
        }
        #endregion
        #region 取消按钮
        private void labelControl2_Click(object sender, EventArgs e)
        {
            Dispose();
        }
        #endregion
        #region 登录
        /// <summary>
        /// 登录
        /// </summary>
        private void GetLogin()
        {
            try
            {
                if (string.IsNullOrEmpty(tbxUserName.Text))
                {
                    MessageBox.Show("用户名必须填写！");
                    tbxUserName.Focus();
                    tbxUserName.SelectAll();
                    return;
                }
                if (string.IsNullOrEmpty(tbxPassword.Text))
                {
                    MessageBox.Show("密码必须填写！");
                    tbxPassword.Focus();
                    tbxPassword.SelectAll();
                    return;
                }
                else
                {
                    Login();
                }
                

            }
            catch (Exception)
            {

                throw;
            }


        }
        #endregion
        #region 登录过程
        private void Login()
        {
            try
            {
                Share.AppClose = true;
                if (string.IsNullOrEmpty(tbxUserName.Text))
                {
                    MessageBox.Show("用户名必须填写！");
                    return;
                }
                if (string.IsNullOrEmpty(tbxPassword.Text))
                {
                    MessageBox.Show("密码必须填写！");
                    return;
                }
                string uPass = CryptUtil.Crypt.MD32Crypt(tbxPassword.Text.Trim());
                string uName = tbxUserName.Text.Trim();
                UserInfoModel uim = new UserInfoModel();
                uim.LoginName = uName;
                uim.Password = uPass;
                RetModel rm = Share.PostData("51API/Manager/ManagerLogin", null, JsonConvert.SerializeObject(uim));
                if (rm.Success)
                {
                    Share.AppClose = false;
                    Share.UInfo = JsonConvert.DeserializeObject<UserInfoModel>(rm.Data.ToString());
                    Dispose();
                }
                else
                {
                    MessageBox.Show("登录未成功！\r\n错误代码:" + rm.ErrCode + "\r\n错误信息：" + rm.ErrInfo);
                    
                    tbxPassword.Text = "";
                    tbxPassword.Focus();
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
    }
}
